/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Autogenerated file -- DO NOT EDIT!

#include "libabckit/src/adapter_dynamic/runtime_adapter_dynamic.h"

namespace libabckit {

% additional_deprecated_opcodes = ["JNSTRICTEQUNDEFINED",
%                                 "JSTRICTEQUNDEFINED",
%                                 "JNEUNDEFINED",
%                                 "JEQUNDEFINED",
%                                 "JNSTRICTEQNULL",
%                                 "JSTRICTEQNULL",
%                                 "JNENULL",
%                                 "JEQNULL",
%                                 "JNSTRICTEQZ",
%                                 "JSTRICTEQZ",
%                                 "STTOGLOBALRECORD",
%                                 "STCONSTTOGLOBALRECORD",
%                                 "CREATEREGEXPWITHLITERAL",
%                                 "CLOSEITERATOR"]

% replaced_opcodes = ["JNSTRICTEQ",
%                    "JSTRICTEQ",
%                    "JNE",
%                    "JEQ"]

% skipped_opcodes = additional_deprecated_opcodes + replaced_opcodes

// NOLINTNEXTLINE(readability-function-size)
static inline AbckitIsaApiDynamicOpcode GetDYNIntrinsicOpcode(ark::compiler::IntrinsicInst *inst) {
    switch(inst->GetIntrinsicId()) {
% Panda::instructions.select{|b| b.namespace == "ecmascript"}.each do |inst|
        case ark::compiler::RuntimeInterface::IntrinsicId::DYN_<%= inst.opcode.upcase %>: {
%   if (inst.mnemonic.include? "deprecated") || (skipped_opcodes.include? inst.mnemonic.upcase)
            return ABCKIT_ISA_API_DYNAMIC_OPCODE_INVALID;
%   else
            return ABCKIT_ISA_API_DYNAMIC_OPCODE_<%= inst.mnemonic.gsub('.', '_').upcase%>;
%   end
        }
% end
        default: {
            return ABCKIT_ISA_API_DYNAMIC_OPCODE_INVALID;
        }
    }
}

// NOLINTNEXTLINE(readability-function-size)
static inline size_t GetIntrinicDYNMaxInputsCount(ark::compiler::IntrinsicInst *inst) {
    switch(inst->GetIntrinsicId()) {
% Panda::instructions.select{|b| b.namespace == "ecmascript"}.each do |inst|
%   if !(inst.mnemonic.include? "deprecated") && !(skipped_opcodes.include? inst.mnemonic.upcase)
        case ark::compiler::RuntimeInterface::IntrinsicId::DYN_<%= inst.opcode.upcase %>:
%     if (inst.mnemonic.include? "range")
            return -1;
%     else
%       num_vregs = inst.operands.select{|b| b.reg?}.length
%       num_inputs = inst.acc.include?("in") ? num_vregs + 1 : num_vregs + 0
            return <%= num_inputs %>;
%     end
%   end
% end
        default:
            LIBABCKIT_UNREACHABLE;
    }
}

// NOLINTNEXTLINE(readability-function-size)
static inline bool IsCallDYNInst(ark::compiler::Inst *inst) {
    if (!inst->IsIntrinsic()) {
        return inst->IsCall();
    }
    switch(inst->CastToIntrinsic()->GetIntrinsicId()) {
% Panda::instructions.select{|b| b.namespace == "ecmascript"}.each do |inst|
        case ark::compiler::RuntimeInterface::IntrinsicId::DYN_<%= inst.opcode.upcase %>: {
%   mnemonic = inst.mnemonic.split(/\s|\./)
%   if ((mnemonic.length() > 1) && (mnemonic[0] == "callruntime") && ((mnemonic[1]).include? "call"))
            return true;
        }
%       next
%   end
%   desc = inst.description.split
%   if (desc.length() > 0 && desc[0] == "call")
            return true;
%   else
            return false;
%   end
        }
% end
        case ark::compiler::RuntimeInterface::IntrinsicId::INTRINSIC_ABCKIT_LOAD_STRING: {
            return false;
        }
        default: {
            LIBABCKIT_UNREACHABLE;
        }
    }
}

}   // libabckit
